<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./test1.css">
    <link rel="stylesheet" href="./test2.css">
    
</head>

<body>
    <div>hello world!</div>
    <script>
        function sendAjax(method,url,flagAsync=true) {
            return new Promise((resolve,reject)=>{
                const xhr=new XMLHttpRequest()
                xhr.open(method,url,flagAsync)

                // 如果flagAsync为true，则是异步的，先写send还是onreadystatechange都可以
                // 否则，得先写onreadystatechange
                // xhr.send()  
                xhr.onreadystatechange=function(){
                    if((this.status==200||this.status==304)&&this.readyState==4){
                        const res=JSON.parse(xhr.responseText)
                        resolve(res)
                    }else if((this.status!=200&&this.status!=304)&&this.readyState==4){
                        reject({
                            response:this.response,
                            code:this.status
                        })
                    }
                }

                xhr.send()
            })
        }

        // sendAjax('get', 'https://cnodejs.org/api/v1//topics')
        sendAjax('get', './test.json')
            .then(res => {
                console.log(res);
            })
        
    </script>


</body>

</html>